//
//  PasswordView.swift
//  KoiAccounting
//
//  Created by 纪洪文 on 17.03.25.
//

import SwiftUI

struct LoginPasswordView: View {
    @Environment(\.presentationMode) var presentationMode;
    @Environment(\.presentToast) var presentToast
    @EnvironmentObject var appState: AppState
    
    // 手机号长度限制
    private let maxPhoneLenght = 13
    
    @State private var phone:String = ""
    @State private var password:String = ""
    @State var toLoginPhone:Bool = false
    @State var privacyChecked:Bool = false
    @State var isShowPrivacyDialog:Bool = false
    var switchLoginView:()->Void
    
    @StateObject private var viewModel = LoginPasswordViewModel()
    
    var body: some View {
        ZStack {
            VStack {
                HStack {
                    Button (action: {
                        presentationMode.wrappedValue.dismiss()
                    }) {
                        Image("koi_ic_close").padding(EdgeInsets(top: 25, leading: 45, bottom: 0, trailing: 0))
                    }
                    Spacer()
                }
                Text("密码登录")
                    .font(.system(size: 25))
                    .bold()
                    .foregroundColor(Color.koiColorTitle)
                    .padding(EdgeInsets(top: 30, leading: 0, bottom: 0, trailing: 0))
                Text("")
                    .font(.system(size: 13))
                    .foregroundColor(Color.koiColorTextAssist)
                    .padding(EdgeInsets(top: 5, leading: 0, bottom: 10, trailing: 0))
                VStack {
                    TextField("请输入手机号", text: $phone)
                        .padding(EdgeInsets(top: 16, leading: 25, bottom: 16, trailing: 25))
                        .background(Color.koiColorBackgroundItem)
                        .cornerRadius(40)
                        .keyboardType(.numberPad)
                        .frame(width: 272)
                        .onChange(of: phone) { value in
                            if value.count > maxPhoneLenght {
                                phone = String(value.prefix(maxPhoneLenght))
                            }
                        }
                    SecureField("请输入密码", text: $password)
                        .padding(EdgeInsets(top: 16, leading: 25, bottom: 16, trailing: 25))
                        .background(Color.koiColorBackgroundItem)
                        .cornerRadius(40)
                        .frame(width: 272)
                        .padding(.top, 6)
                        
                    HStack {
                        Button(action:{
                            switchLoginView()
                        }) {
                            Image("koi_ic_arrow_switch")
                                .renderingMode(.template)
                                .foregroundColor(Color.koiColorTitleSecond)
                            Text("验证码登录")
                                .font(.system(size: 13))
                                .foregroundColor(Color.koiColorTitleSecond)
                        }
                        Spacer()
                    }.padding(EdgeInsets(top: 5, leading: 10, bottom: 0, trailing: 0))
                }.frame(width: 272)
                    .padding(EdgeInsets(top: 20, leading: 0, bottom: 0, trailing: 0))
                Button(action:{
                    
                }) {
                    Button(action:{
                        passwordLogin()
                    }) {
                        Text("登录")
                            .font(.system(size: 16))
                            .bold()
                            .padding(EdgeInsets(top: 16, leading: 0, bottom: 16, trailing: 0))
                            .frame(width: 272)
                            .foregroundColor(Color.white)
                            .background(Color.koiColorPrimary)
                            .cornerRadius(40)
                            .padding(.top, 20)
                    }
                }
                PrivacyCheckView(isChecked: $privacyChecked)
                Spacer()
            }
            if viewModel.isLoading {
                ProgressView()
            }
        }.koiSheet(isPresented: $isShowPrivacyDialog) {
            PrivacyConfirmDialog(
                isShowPrivacyDialog:$isShowPrivacyDialog,
                onConfirmClick: {
                    privacyChecked.toggle()
                    isShowPrivacyDialog.toggle()
                    passwordLogin()
                })
        }
        .onChange(of: viewModel.loginState) { value in
            if value == "success" {
                viewModel.loginState = ""
                appState.isLoggedIn = true
            }
        }
    }
    
    /**
     密码登录
     */
    func passwordLogin() {
        if phone.isEmpty {
            presentToast(.init(message: "请输入手机号"))
            return
        }
        if password.isEmpty {
            presentToast(.init(message: "请输入密码"))
            return
        }
        if (!checkPrivacy()) {
            return
        }
        
        viewModel.login(phone: phone, password: password)
    }
    
    private func checkPrivacy() -> Bool {
        if (!privacyChecked) {
            isShowPrivacyDialog.toggle()
        }
        return privacyChecked
    }
}

#Preview {
    LoginPasswordView(switchLoginView: {})
}
